package cc.rengu.igas.momp.biz.service.impl;

import cc.rengu.igas.momp.common.enums.MompTxnNumEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.OutChannelCouponMcService;
import cc.rengu.igas.momp.facade.request.ClaimCouponOutChannelRequest;
import cc.rengu.igas.momp.facade.request.QueryCouponInfoByCustomerRequest;
import cc.rengu.igas.momp.facade.request.QueryMcListWithDetialInfoRequest;
import cc.rengu.igas.momp.facade.response.ClaimCouponOutChannelResponse;
import cc.rengu.igas.momp.facade.response.QueryCouponInfoByCustomerResponse;
import cc.rengu.igas.momp.facade.response.QueryMcListWithDetialInfoResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;

/**
 * 外部渠道卡券营销活动管理
 *
 * @author liujinan
 * @version 1.0.0
 * @date 2021/1/19 11:53
 */
@SuppressWarnings("unchecked")
public class OutChannelCouponMcServiceImpl implements OutChannelCouponMcService {
    /**
     * 卡券类营销活动列表查询接口Dubbo接口实现
     *
     * @param queryMcListWithDetialInfoRequest 卡券类营销活动列表查询接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryMemberInfoResponse> 会员信息查询接口应答对象
     * @author liujinan
     * @date 2021/1/19 15:17
     */
    @Override
    public Result<QueryMcListWithDetialInfoResponse> queryMcListWithDetialInfo(QueryMcListWithDetialInfoRequest queryMcListWithDetialInfoRequest) {

        DubboService<QueryMcListWithDetialInfoRequest> dubboService = new DubboServiceImpl();
        Result<QueryMcListWithDetialInfoResponse> queryMcListWithDetialInfoResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.QUERY_MCLIST_WITHDETIAL_INFO.getTxnNum().equals(queryMcListWithDetialInfoRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryMcListWithDetialInfoResponse> dubboResult = dubboService.callAtomService(queryMcListWithDetialInfoRequest,
                    MompTxnNumEnum.QUERY_MCLIST_WITHDETIAL_INFO.getService(), QueryMcListWithDetialInfoResponse.class);
            queryMcListWithDetialInfoResponseResult.setResult(dubboResult.getResult());
            queryMcListWithDetialInfoResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryMcListWithDetialInfoResponse queryMcListWithDetialInfoResponse = new QueryMcListWithDetialInfoResponse();
            queryMcListWithDetialInfoResponseResult.setResult(queryMcListWithDetialInfoResponse);
            queryMcListWithDetialInfoResponse.setHeader(queryMcListWithDetialInfoRequest.getHeader());
            queryMcListWithDetialInfoResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryMcListWithDetialInfoResponse.setRespCode(((BizException) e).getCode());
                queryMcListWithDetialInfoResponse.setRespDesc(e.getMessage());
            } else {
                queryMcListWithDetialInfoResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryMcListWithDetialInfoResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryMcListWithDetialInfoResponseResult;
    }

    /**
     * 用户卡券领取Dubbo实现
     *
     * @param claimCouponOutChannelRequest 用户卡券领取接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.ClaimCouponOutChannelResponse> 用户卡券领取接口应答对象
     * @author liujinan
     * @date 2021/1/19 15:17
     */
    @Override
    public Result<ClaimCouponOutChannelResponse> customerClaimCoupon(ClaimCouponOutChannelRequest claimCouponOutChannelRequest) {

        DubboService<ClaimCouponOutChannelRequest> dubboService = new DubboServiceImpl();
        Result<ClaimCouponOutChannelResponse> ClaimCouponOutChannelResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.CLAIM_COUPON_OUTCHANNEL.getTxnNum().equals(claimCouponOutChannelRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ClaimCouponOutChannelResponse> dubboResult = dubboService.callAtomService(claimCouponOutChannelRequest,
                    MompTxnNumEnum.CLAIM_COUPON_OUTCHANNEL.getService(), ClaimCouponOutChannelResponse.class);
            ClaimCouponOutChannelResponseResult.setResult(dubboResult.getResult());
            ClaimCouponOutChannelResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ClaimCouponOutChannelResponse claimCouponOutChannelResponse = new ClaimCouponOutChannelResponse();
            ClaimCouponOutChannelResponseResult.setResult(claimCouponOutChannelResponse);
            claimCouponOutChannelResponse.setHeader(claimCouponOutChannelRequest.getHeader());
            ClaimCouponOutChannelResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                claimCouponOutChannelResponse.setRespCode(((BizException) e).getCode());
                claimCouponOutChannelResponse.setRespDesc(e.getMessage());
            } else {
                claimCouponOutChannelResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                claimCouponOutChannelResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return ClaimCouponOutChannelResponseResult;
    }

    /**
     * 用户卡券查询Dubbo实现
     *
     * @param queryCouponInfoByCustomerRequest 用户卡券查询接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryCouponInfoByCustomerResponse> 用户卡券查询接口应答对象
     * @author liujinan
     * @date 2021/1/19 15:16
     */
    @Override
    public Result<QueryCouponInfoByCustomerResponse> customerQueryCouponInfo(QueryCouponInfoByCustomerRequest queryCouponInfoByCustomerRequest) {

        DubboService<QueryCouponInfoByCustomerRequest> dubboService = new DubboServiceImpl();
        Result<QueryCouponInfoByCustomerResponse> queryCouponInfoByCustomerResponseRequest = new Result<>();
        try {
            if (!MompTxnNumEnum.QUERY_COUPONINFO_BYCUSTOMER.getTxnNum().equals(queryCouponInfoByCustomerRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryCouponInfoByCustomerResponse> dubboResult = dubboService.callAtomService(queryCouponInfoByCustomerRequest,
                    MompTxnNumEnum.QUERY_COUPONINFO_BYCUSTOMER.getService(), QueryCouponInfoByCustomerResponse.class);
            queryCouponInfoByCustomerResponseRequest.setResult(dubboResult.getResult());
            queryCouponInfoByCustomerResponseRequest.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryCouponInfoByCustomerResponse queryMemberCouponInfoResponse = new QueryCouponInfoByCustomerResponse();
            queryCouponInfoByCustomerResponseRequest.setResult(queryMemberCouponInfoResponse);
            queryMemberCouponInfoResponse.setHeader(queryCouponInfoByCustomerRequest.getHeader());
            queryCouponInfoByCustomerResponseRequest.setSuccess(false);
            if (e instanceof BizException) {
                queryMemberCouponInfoResponse.setRespCode(((BizException) e).getCode());
                queryMemberCouponInfoResponse.setRespDesc(e.getMessage());
            } else {
                queryMemberCouponInfoResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryMemberCouponInfoResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryCouponInfoByCustomerResponseRequest;
    }
}
